﻿namespace WMS6.Web.OrderManagement
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OrderMgtServiceRef;
    using WMS6.Web.UserControls;

    public class OnLinePOList : BasePage
    {
        protected Button btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnFinish;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnRefresh;
        protected Button btnReport;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F3041;
        protected GridView gvPo;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblSalesOrder;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2SalesOrderList(PurchaseOrderQueryEntity purchaseOrderQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(purchaseOrderQueryEntity.CurrentPage);
            DataSet ds = new DataSet();
            ds = OrderMgtServiceClientProxy.GetInstance().GetPurchaseOrderInfoByQueryEntity(purchaseOrderQueryEntity);
            base.BindData2GridView(this.gvPo, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPo, "PO_ID");
                List<string> listStatus = base.GetSelectedIDsInGridView(this.gvPo, "STATUS");
                if ((listStatus != null) || (listStatus.Count != 0))
                {
                    for (int i = 0; i < listStatus.Count; i++)
                    {
                        if (listStatus[i].ToString().ToUpper() != "NEW")
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F3100_006");
                            return;
                        }
                    }
                }
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    this.lblMsg.Text = OrderMgtServiceClientProxy.GetInstance().DeletePurchaseOrderInfo(selectedID) + base.GetGlobalMsgResource("C006");
                    PurchaseOrderQueryEntity purchaseOrderQueryEntity = this.GetPurchaseOrderQueryEntity();
                    this.BindData2SalesOrderList(purchaseOrderQueryEntity);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPo, "PO_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                base.Response.Redirect(string.Format("OnLinePO.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            PurchaseOrderQueryEntity purchaseOrderQueryEntity = this.GetPurchaseOrderQueryEntity();
            purchaseOrderQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = purchaseOrderQueryEntity.CurrentPage;
            purchaseOrderQueryEntity.CurrentPage = "0";
            this.gvPo.Columns[1].Visible = false;
            this.gvPo.AllowSorting = false;
            this.BindData2SalesOrderList(purchaseOrderQueryEntity);
            this.gvPo.ShowFooter = false;
            purchaseOrderQueryEntity.IsGetAll = false;
            purchaseOrderQueryEntity.CurrentPage = currentPageIndex;
            this.SetPurchaseOrderQueryEntity(purchaseOrderQueryEntity);
            this.hdnActionID.Value = "";
            base.Export2Excel("OnLinePO.xls", this.gvPo);
        }

        protected void btnFinish_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPo, "PO_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                this.hdnRtnValue.Value = selectedID[0];
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            PurchaseOrderQueryEntity purchaseOrderQueryEntity = this.GetPurchaseOrderQueryEntity();
            purchaseOrderQueryEntity.CurrentPage = "0";
            this.BindData2SalesOrderList(purchaseOrderQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            PurchaseOrderQueryEntity purchaseOrderQueryEntity = this.GetPurchaseOrderQueryEntity();
            purchaseOrderQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2SalesOrderList(purchaseOrderQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            PurchaseOrderQueryEntity purchaseOrderQueryEntity = this.GetPurchaseOrderQueryEntity();
            purchaseOrderQueryEntity.PoID = "";
            purchaseOrderQueryEntity.OwnerID = "";
            purchaseOrderQueryEntity.VendorID = "";
            purchaseOrderQueryEntity.PoDate = "";
            purchaseOrderQueryEntity.RequestDeliveryDate = "";
            purchaseOrderQueryEntity.PoType = "";
            purchaseOrderQueryEntity.ExternalPoID = "";
            purchaseOrderQueryEntity.Status = "";
            purchaseOrderQueryEntity.CreatedBy = base.GetParamsString(base.UserInfo.UserID, "Same As");
            this.BindData2SalesOrderList(purchaseOrderQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            PurchaseOrderQueryEntity purchaseOrderQueryEntity = this.GetPurchaseOrderQueryEntity();
            if (int.Parse(purchaseOrderQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                purchaseOrderQueryEntity.CurrentPage = (int.Parse(purchaseOrderQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2SalesOrderList(purchaseOrderQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            PurchaseOrderQueryEntity purchaseOrderQueryEntity = this.GetPurchaseOrderQueryEntity();
            if (int.Parse(purchaseOrderQueryEntity.CurrentPage) > 0)
            {
                purchaseOrderQueryEntity.CurrentPage = (int.Parse(purchaseOrderQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2SalesOrderList(purchaseOrderQueryEntity);
            }
        }

        private PurchaseOrderQueryEntity GetPurchaseOrderQueryEntity()
        {
            return (base.PageQueryEntitySession as PurchaseOrderQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                PurchaseOrderQueryEntity purchaseOrderQueryEntity = this.GetPurchaseOrderQueryEntity();
                purchaseOrderQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2SalesOrderList(purchaseOrderQueryEntity);
            }
        }

        protected void gvPo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvPo_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string strValue = base.UrlReplace(this.gvPo.DataKeys[e.NewSelectedIndex]["PO_ID"].ToString());
            if (!string.IsNullOrEmpty(strValue))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + strValue + "';  window.close();</script>", false);
            }
        }

        protected void gvPo_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                PurchaseOrderQueryEntity purchaseOrderQueryEntity = this.GetPurchaseOrderQueryEntity();
                purchaseOrderQueryEntity.SortField = e.SortExpression;
                if (purchaseOrderQueryEntity.SortDirection == "ASC")
                {
                    purchaseOrderQueryEntity.SortDirection = "DESC";
                }
                else if (purchaseOrderQueryEntity.SortDirection == "DESC")
                {
                    purchaseOrderQueryEntity.SortDirection = "ASC";
                }
                this.BindData2SalesOrderList(purchaseOrderQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            PurchaseOrderQueryEntity purchaseOrderQueryEntity = new PurchaseOrderQueryEntity();
            if ((string.IsNullOrEmpty(base.Request.QueryString["from"]) || (base.Request.QueryString["from"] == "query")) || (base.Request.QueryString["from"] == "list"))
            {
                if (this.PreviousPage != null)
                {
                    purchaseOrderQueryEntity.PoID = this.PreviousPage.poID;
                    purchaseOrderQueryEntity.OwnerID = this.PreviousPage.OwnerID;
                    purchaseOrderQueryEntity.VendorID = this.PreviousPage.VendorID;
                    purchaseOrderQueryEntity.PoDate = this.PreviousPage.PoDate;
                    purchaseOrderQueryEntity.RequestDeliveryDate = this.PreviousPage.RequestDeliveryDate;
                    purchaseOrderQueryEntity.PoType = "= 'Online'";
                    purchaseOrderQueryEntity.ExternalPoID = this.PreviousPage.ExternalPoID;
                    purchaseOrderQueryEntity.Status = this.PreviousPage.Status;
                    purchaseOrderQueryEntity.CreatedBy = this.PreviousPage.CreatedBy;
                }
                else
                {
                    purchaseOrderQueryEntity.PoID = "";
                    purchaseOrderQueryEntity.OwnerID = "";
                    purchaseOrderQueryEntity.VendorID = "";
                    purchaseOrderQueryEntity.PoDate = "";
                    purchaseOrderQueryEntity.RequestDeliveryDate = "";
                    purchaseOrderQueryEntity.PoType = "= 'Online'";
                    purchaseOrderQueryEntity.ExternalPoID = "";
                    purchaseOrderQueryEntity.Status = "";
                    purchaseOrderQueryEntity.CreatedBy = base.GetParamsString(base.UserInfo.UserID, "Same As");
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if ((this.hdnFrom.Value.ToLower().Trim() != "edit") || !string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                purchaseOrderQueryEntity.IsGetAll = false;
                purchaseOrderQueryEntity.CurrentPage = "0";
                purchaseOrderQueryEntity.PageSize = this.gvPo.PageSize.ToString();
                purchaseOrderQueryEntity.SortField = "PO_ID";
                purchaseOrderQueryEntity.SortDirection = "DESC";
                this.PagerFooter.PageSize = this.gvPo.PageSize;
                if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
                {
                    this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                    this.btnFinish.Visible = false;
                    this.gvPo.Columns[1].Visible = false;
                    this.gvPo.Columns[2].Visible = true;
                    this.btnNew.Visible = false;
                    this.btnEdit.Visible = false;
                    this.btnDelete.Visible = false;
                    this.btnAction.Visible = false;
                    this.btnReSearch.Visible = false;
                    this.btnExport.Visible = false;
                    this.btnReport.Visible = false;
                }
                if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
                {
                    this.hdnActionID.Value = base.Request.QueryString["export"];
                }
                this.SetPurchaseOrderQueryEntity(purchaseOrderQueryEntity);
            }
            else
            {
                purchaseOrderQueryEntity = this.GetPurchaseOrderQueryEntity();
            }
            this.BindData2SalesOrderList(purchaseOrderQueryEntity);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetPurchaseOrderQueryEntity(PurchaseOrderQueryEntity purchaseOrderQueryEntity)
        {
            base.PageQueryEntitySession = purchaseOrderQueryEntity;
        }

        public OnLinePOQuery PreviousPage
        {
            get
            {
                return (OnLinePOQuery) base.PreviousPage;
            }
        }
    }
}

